<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台控制器演示。
 *
 **/
namespace Mobile\Controller;

class InterfaceController extends ComController
{
    public function buy(){

        $key = "grainsys";
        if(I('get.key')!=$key){
            $json['status'] = 0;//错误
            echo json_encode($json);exit;
        }

        $Warehouse = M("warehouse");
        $warehouse_array = $Warehouse->select();

        $Foodstuff = M("foodstuff");
        $foodstuff_array = $Foodstuff->select();

        $Cartype = M("cartype");
        $cartype_array = $Cartype->select();

        $json_array = array();
        $json_array['warehouse'] = $warehouse_array;
        $json_array['foodstuff'] = $foodstuff_array;
        $json_array['cartype'] = $cartype_array;

        echo json_encode($json_array);
    }



    public function getParameter(){
//        echo U();
        //http://grainsys.3hz1.com/index.php?s=/Mobile/Interface/getParameter/key/grainsys/foodstuff_id/2/level/1
        //status:0 错误 ,1 正常, 2无数据
        $key = "grainsys";
        if(I('get.key')!=$key){
            $json['status'] = 0;//错误
            echo json_encode($json);exit;
        }
        $where['foodstuff_id'] = I("foodstuff_id");
        $where['level'] = I("level");

        $Parameter = M("parameter");
        $Setting = M("parameter_setting");

        $parameter_res = $Parameter->where($where)->find();
        if(empty($parameter_res)){
            $json['status'] = 2;//无数据
            echo json_encode($json);
        }else{
            $map['parameter_id'] = $parameter_res['id'];
            $setting_res = $Setting->where($map)->find();

            $shuifen_array = unserialize($setting_res['shuifen']);
            $shengmei_array = unserialize($setting_res['shuifen']);
            $zazhi_array = unserialize($setting_res['zazhi']);
            $chucaolv_array = unserialize($setting_res['chucaolv']);
            $zhengjingmilv_array = unserialize($setting_res['zhengjingmilv']);
            $guwaicaomilv_array = unserialize($setting_res['guwaicaomilv']);
            $huanglimi_array = unserialize($setting_res['huanglimi']);
            $data = array();
            $data['parameter'] = $parameter_res;
            $data['shuifen'] = $shuifen_array;
            $data['shengmei'] = $shengmei_array;
            $data['zazhi'] = $zazhi_array;
            $data['chucaolv'] = $chucaolv_array;
            $data['zhengjingmilv'] = $zhengjingmilv_array;
            $data['guwaicaomilv'] = $guwaicaomilv_array;
            $data['huanglimi'] = $huanglimi_array;

            $json['status'] = 1;//正常
            $json['data'] = $data;

            echo json_encode($json);
        }

    }

    public function buyAdd(){
        $key = "grainsys";
        if(I('get.key')!=$key){
            $json['status'] = 0;//错误
            echo json_encode($json);exit;
        }

        $Buyvoucher = D("buyvoucher");
        $data = I('post.');


        if(!isNumber2($data['price'])){
            $json['status'] = 2;
            $json['str'] = "单价数值填写有误";
            echo json_encode($json);exit;
        }

        if(!isNumber2($data['jingzhong'])){
            $json['status'] = 2;
            $json['str'] = "净重填写有误";
            echo json_encode($json);exit;
        }

        if(!isNumber2($data['shijijiakuan'])){
            $json['status'] = 2;
            $json['str'] = "实际价款填写有误";
            echo json_encode($json);exit;
        }

            $id = $Buyvoucher->add($data);
            if(!$id){
                $json['status'] = 2;
                $json['str'] = "凭证添加失败！";
            }else{
                $json['status'] = 1;
                $json['str'] = "凭证添加成功！";
            }
            echo json_encode($json);exit;
//        if(!$Buyvoucher->create()){
//            $json['status'] = 2;
//            $json['str'] = $Buyvoucher->getError();
//        }else{
//            $id = $Buyvoucher->add();
//            if(!$id){
//                $json['status'] = 2;
//                $json['str'] = "凭证添加失败！";
//            }else{
//                $json['status'] = 1;
//                $json['str'] = "凭证添加成功！";
//            }
//
//        }
        echo json_encode($json);exit;
    }

    public function sell(){
        // http://grainsys.3hz1.com/index.php?s=/Mobile/Interface/sell
        $key = "grainsys";
        if(I('get.key')!=$key){
            $json['status'] = 0;//错误
            echo json_encode($json);exit;
        }
        $Foodstuff = M("foodstuff");
        $foodstuff_array = $Foodstuff->select();
        $json_array = array();
        $json_array['foodstuff'] = $foodstuff_array;
        echo json_encode($json_array);
    }

    public function selladd(){
        // http://grainsys.3hz1.com/index.php?s=/Mobile/Interface/selladd

        $key = "grainsys";
        if(I('get.key')!=$key){
            $json['status'] = 0;//错误
            echo json_encode($json);exit;
        }

        $Sellvoucher = M("sellvoucher");
        $data = I("post.");

        if(!isNumber2($data['price'])){
            $json['status'] = 2;
            $json['str'] = "单价填写有误";
            echo json_encode($json);exit;
        }
        if(!isNumber2($data['jingzhong'])){
            $json['status'] = 2;
            $json['str'] = "净重填写有误";
            echo json_encode($json);exit;
        }

        if(!isNumber2($data['shijijine'])){
            $json['status'] = 2;
            $json['str'] = "实际金额填写有误";
            echo json_encode($json);exit;
        }

            $id = $Sellvoucher->add($data);
            if(!$id){
                $json['status'] = 2;
                $json['str'] = "凭证添加失败！";
            }else{
                $json['status'] = 1;
                $json['str'] = "凭证添加成功！";
            }
            echo json_encode($json);exit;

    }

    public function getPrice(){
        // http://grainsys.3hz1.com/index.php?s=/Mobile/Interface/getPrice/key/grainsys/foodstuff_id/1/level/2
        $key = "grainsys";
        if(I('get.key')!=$key){
            $json['status'] = 0;//错误
            echo json_encode($json);exit;
        }

        $where['foodstuff_id'] = I("foodstuff_id");
        $where['level'] = I("level");
        $Parameter = M("parameter");
        $res = $Parameter->where($where)->find();
        if(empty($res)){
            $json['status'] = 2;
        }else{
            $json['status'] = 1;
            $json['price'] = $res['price'];
        }
        echo json_encode($json);exit;




    }





}